/*
*  14.3.2011 L��v�k Bjarnason
*
* Copyright (c) L��v�k Bjarnason
*
*/

import java.io.*;
import java.net.Socket;
import java.net.UnknownHostException;

public class HttpRuClient {


    public static void main(String[] args) {
        // First parameter has to be machine name
        if (args.length == 0) {
            System.out.println("Usage : tcp <portnumber> <hostname>");
            return;
        }

        Socket s = null;

        String hostname = args[2];
        int portnumber = Integer.parseInt(args[1]);

        // Create the socket connection to the EchoServer.
        try {
            s = new Socket(hostname, portnumber);
        }
        catch (UnknownHostException uhe) {
            // Host unreachable
            System.out.println("Unknown Host :" + hostname);
            s = null;
        }
        catch (IOException ioe) {
            // Cannot connect to port on given host
            System.out.println("Cant connect to server at " + hostname + ". Make sure it is running.");
            s = null;
        }

        if (s == null)
            System.exit(-1);

        PrintWriter out = null;
        BufferedReader in = null;

        try {
            if (args[0].equalsIgnoreCase("tcp")) {


                // Create the streams to send and receive information

                out = new PrintWriter(s.getOutputStream(), true);

                in = new BufferedReader(new InputStreamReader(
                        s.getInputStream()));
                // run function httpClient that process the http client
                httpClientrun(out, in, s);
            } else if (args[0].equalsIgnoreCase("udp")) {

                UDPClient updClient = new UDPClient(portnumber, hostname);
                updClient.run();

            } else {
                System.out.println("You have entered wrong input: use either tcp or udp");
            }

        }
        catch (IOException ioe) {
            System.out.println("Exception during communication. Server probably closed connection.");
        }


        catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void httpClientrun(PrintWriter out, BufferedReader in, Socket s) throws IOException {

        BufferedReader stdIn = new BufferedReader(
                new InputStreamReader(System.in));
        String userInput;
        String strLine;
        System.out.println("Type in GET command for instance: GET /index.html?allCaps=stne");
        System.out.println("The program will close after you have typed in GET query");
        System.out.println("Press q to quit");
        while (!(userInput = stdIn.readLine()).equalsIgnoreCase("q")) {
            out.println(userInput);
            out.flush();
            while ((strLine = in.readLine()) != null) {
                System.out.println(strLine);
            }

            in.reset();

        }

        out.close();

        stdIn.close();
        s.close();


    }


}



